package com.yizisu.playerlibrary.view.video_view.view

import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import com.yizisu.playerlibrary.IYzsPlayer
import com.yizisu.playerlibrary.databinding.PlayerRootViewBinding
import com.yizisu.playerlibrary.view.video_view.helper.*
import com.yizisu.playerlibrary.view.video_view.helper.PlayerControllerBarHelper.Companion.DEFAULT_DELAY_DURATION

/**
 * 播放器的根view
 */
open class PlayerRootView : BasePlayerRootView, IPlayerHelper {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    private val videoViewBinding by lazy {
        PlayerRootViewBinding.inflate(
            LayoutInflater.from(context),
            this,
            true
        )
    }

    init {
        videoViewBinding.smallPlayPause.enableGoneWhenPlaying = false
    }

    private var playerConfig: IPlayerConfig? = null

    override fun onAttachPlayerConfig(playerConfig: IPlayerConfig) {
        super.onAttachPlayerConfig(playerConfig)
        this.playerConfig = playerConfig
        playerConfig.clickBackData.observeForever(::onClickBack)
        playerConfig.showOrHideControllerData.observeForever(::onShowOrHideBar)
    }

    override fun onDetachedPlayerConfig(playerConfig: IPlayerConfig) {
        super.onDetachedPlayerConfig(playerConfig)
        playerConfig.clickBackData.removeObserver(::onClickBack)
        playerConfig.showOrHideControllerData.removeObserver(::onShowOrHideBar)
    }

    /**
     * 展示或者隐藏导航栏
     */
    private fun onShowOrHideBar(data: Float?) {
        data?.let {
            //region 隐藏或者显示底部导航栏
            if (it != 0f) {
                videoViewBinding.bottomView.visibility = View.VISIBLE
                videoViewBinding.topBar.visibility = View.VISIBLE
                if (videoViewBinding.cacheView.canDownload==true) {
                    videoViewBinding.cacheView.visibility = View.VISIBLE
                }
                videoViewBinding.lockView.visibility = View.VISIBLE
            } else {
                videoViewBinding.bottomView.visibility = View.INVISIBLE
                videoViewBinding.topBar.visibility = View.INVISIBLE
                videoViewBinding.cacheView.visibility = View.INVISIBLE
                videoViewBinding.lockView.visibility = View.INVISIBLE
            }
            val offYBottom = videoViewBinding.bottomView.height * (1 - it)
            val offYTop = -videoViewBinding.bottomView.height * (1 - it)
            if (videoViewBinding.bottomView.height == 0) {
                post {
                    videoViewBinding.bottomView.translationY = offYBottom
                    videoViewBinding.topBar.translationY = offYTop
                }
            } else {
                videoViewBinding.bottomView.translationY = offYBottom
                videoViewBinding.topBar.translationY = offYTop
            }
            //endregion
        }
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        checkScreenLock(ev)
        return super.onInterceptTouchEvent(ev)
    }

    /**
     * 检测屏幕是否可用
     */
    private fun checkScreenLock(ev: MotionEvent?) {
        when (ev?.action) {
            MotionEvent.ACTION_DOWN -> {
                playerConfig?.playerControllerBarHelperData?.value?.hideIfShow(
                    DEFAULT_DELAY_DURATION
                )
                if (playerConfig?.screenNotLockData?.value == false) {
                    videoViewBinding.lockView.postHideWhenLock()
                }
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                if (playerConfig?.fullScreenChangeData?.value == true) {
                    (context as Activity).window.fullScreen(true)
                }
            }
        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        updateLayout()
    }

    override fun onConfigurationChanged(newConfig: Configuration?) {
        super.onConfigurationChanged(newConfig)
        updateLayout()
    }

    private fun updateLayout() {
        val displayMetrics = resources.displayMetrics
        val margin = if (displayMetrics.widthPixels <= displayMetrics.heightPixels) {
            0
        } else {
            dip(32)
        }
        videoViewBinding.topBar.setPadding(
            margin,
            videoViewBinding.topBar.paddingTop,
            margin,
            videoViewBinding.topBar.paddingBottom
        )
        videoViewBinding.bottomView.setPadding(
            margin,
            videoViewBinding.bottomView.paddingTop,
            margin,
            videoViewBinding.bottomView.paddingBottom
        )
    }

    private fun onClickBack(view: View?) {
        (context as Activity).finishAfterTransition()
    }
}